#!/bin/bash

directory="/var/named/"
ip="127.0.0.1"
port="53"
prefix="test"
zone_num=10
rr_count=10

confdir="etc"
datadir="data"
logdir="log"
dstdir="."

usage() {
    echo "Usage:"
    echo -e "\t-d directory:\t\tspecify remote bind main directory"
    echo -e "\t-a ip_addr  :\t\tspecify remote bind listened ip address"
    echo -e "\t-p port     :\t\tspecify remote bind listened port"
    echo -e "\t-f prefix   :\t\tspecify prefix in each zone name"
    echo -e "\t-n zone_num :\t\tspecify zone number"
    echo -e "\t-c rr_count :\t\tspecify rr count in each zone"
    echo -e "\t-r dstdir   :\t\tspecify directory store .tgz file"
    echo -e "\t-h          :\t\tprint help info"
}

if [ $# -ne 1 -a $# -ne 14 ]
then
    usage
    exit
fi

args=`getopt -o hdr:a:c:p:z:n:f: -l directory:,address:,port:,zone:,num:,rr_count:,prefix:,dstdir:,help -- $*`

if [ $? -ne 0 ]
then
    usage
    exit
fi

while getopts d:r:a:p:f:n:c: OPTION
do
    case $OPTION
    in
        d)directory=$OPTARG;;
        a)ip=$OPTARG;;
        p)port=$OPTARG;;
        f)prefix=$OPTARG;;
        n)zone_num=$OPTARG;;
        c)rr_count=$OPTARG;;
        r)dstdir=$OPTARG;;
        h)
            usage
            exit;;
        /?)echo "invalid parameters"
           exit;;
    esac
done

if [ 'xx'$dstdir == 'xx' ] ; then
   dstdir=`pwd`
fi

zone_num=`expr ${zone_num} - 1`
rr_count=`expr ${rr_count} - 1`

#clean
cur_dir=`pwd`
qp_file="${cur_dir}/qp_data"
rm -rf ${confdir} ${datadir} ${logdir} ${qp_file}

#conf file
conf_file="named.conf"
fullpath_conf_file="${cur_dir}/${confdir}/${conf_file}"
mkdir ${confdir}
cd ${confdir}

#option
echo -e "options {" >> ${fullpath_conf_file}
echo -e "\tdirectory \"${directory}\";" >> ${fullpath_conf_file}
echo -e "\tlisten-on port ${port} { ${ip}; };" >> ${fullpath_conf_file}
echo -e "\tallow-query { any; };" >> ${fullpath_conf_file}
echo -e "\trecursion no;" >> ${fullpath_conf_file}
echo -e "};" >> ${fullpath_conf_file}
echo -e "\n" >> ${fullpath_conf_file}

#log
echo -e "logging {" >> ${fullpath_conf_file}
echo -e "\tchannel general_log {" >> ${fullpath_conf_file}
echo -e "\t\tfile \"${directory}/${logdir}/general_log\" versions 100 size 10m;" >> ${fullpath_conf_file}
echo -e "\t\tseverity info;" >> ${fullpath_conf_file}
echo -e "\t\tprint-category yes;" >> ${fullpath_conf_file}
echo -e "\t\tprint-severity yes;" >> ${fullpath_conf_file}
echo -e "\t\tprint-time yes;" >> ${fullpath_conf_file}
echo -e "\t};" >> ${fullpath_conf_file}
echo -e "\n" >> ${fullpath_conf_file}

echo -e "\tchannel default_log {" >> ${fullpath_conf_file}
echo -e "\t\tfile \"${directory}/${logdir}/default_log\" versions 100 size 10m;" >> ${fullpath_conf_file}
echo -e "\t\tseverity info;" >> ${fullpath_conf_file}
echo -e "\t\tprint-category yes;" >> ${fullpath_conf_file}
echo -e "\t\tprint-severity yes;" >> ${fullpath_conf_file}
echo -e "\t\tprint-time yes;" >> ${fullpath_conf_file}
echo -e "\t};" >> ${fullpath_conf_file}
echo -e "\n" >> ${fullpath_conf_file}

echo -e "\tchannel query_log {" >> ${fullpath_conf_file}
echo -e "\t\tfile \"${directory}/${logdir}/query_log\" versions 100 size 10m;" >> ${fullpath_conf_file}
echo -e "\t\tseverity info;" >> ${fullpath_conf_file}
echo -e "\t\tprint-category no;" >> ${fullpath_conf_file}
echo -e "\t\tprint-severity no;" >> ${fullpath_conf_file}
echo -e "\t\tprint-time yes;" >> ${fullpath_conf_file}
echo -e "\t};" >> ${fullpath_conf_file}
echo -e "\n" >> ${fullpath_conf_file}

echo -e "\t#category queries { query_log; };" >> ${fullpath_conf_file}
echo -e "\tcategory default { default_log; };" >> ${fullpath_conf_file}
echo -e "\tcategory general { general_log; };" >> ${fullpath_conf_file}
echo -e "};" >> ${fullpath_conf_file}
echo -e "\n" >> ${fullpath_conf_file}

#data file
cd ${cur_dir}
mkdir ${datadir}
cd ${datadir}
for i in `seq -w 0 ${zone_num}`
do
    zone_name="${prefix}${i}"
    zone_file="${zone_name}.txt"

    echo -e "zone \"${zone_name}\" {" >> ${fullpath_conf_file}
    echo -e "\ttype master;" >> ${fullpath_conf_file}
    echo -e "\tfile \"${directory}/${datadir}/${zone_file}\";" >> ${fullpath_conf_file}
    echo -e "};\n" >> ${fullpath_conf_file}

    fullpath_zone_file="${cur_dir}/${datadir}/${zone_file}"
    echo "${zone_name}. 3600 SOA ns.${zone_name}. root.${zone_name}. 12345 1800 900 604800 86400" >> ${fullpath_zone_file}
    echo "${zone_name}. 3600 NS  ns.${zone_name}." >> ${fullpath_zone_file}
    echo "ns.${zone_name}. 3600 A 127.0.0.1" >> ${fullpath_zone_file}
    for j in `seq -w 0 ${rr_count}`
    do
        echo "${j}.${zone_name}. 3600 A 10.0.0.1" >> ${fullpath_zone_file}
        echo "${j}.${zone_name}. A" >> ${qp_file}
    done
done

#log file
cd ${cur_dir}
mkdir ${logdir}

#compress
cd ${cur_dir}
tar cvfz $dstdir/multi_zone.tgz ${confdir} ${datadir} ${logdir} -C $dstdir
